package com.wangrui.us3.s3;

import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.model.Bucket;
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest;
import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.time.Duration;

public class Test1 {
    public static void main(String[] args) throws IOException {
        String bucketName = "test-gboss-kj";
        String keyName = "万相2.5_1759110999134.mp4";
        String filePath = "C:\\Users\\wangrui\\Downloads\\万相 2.5_1759223272439.mp4";
        // 创建 S3Presigner 实例
        try (S3Presigner presigner = S3Util.getPreDesigner()) {

            // 创建预签名请求
            PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                    .bucket(bucketName)
                    .key(keyName)
                    .build();

            PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder()
                    .signatureDuration(Duration.ofMinutes(10)) // 设置有效期 10 分钟
                    .putObjectRequest(putObjectRequest)
                    .build();

            try {
                // 获取预签名 URL
                PresignedPutObjectRequest presignedRequest = presigner.presignPutObject(presignRequest);
                String presignedUrl = presignedRequest.url().toString();
                System.out.println("Presigned URL to upload a file to: " + presignedUrl);

                // 上传文件
                uploadFileUsingHttpURLConnection(presignedUrl, filePath);
            } catch (Exception e) {
                System.err.println("生成预签名URL时出错: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    public static void uploadFileUsingHttpURLConnection(String presignedUrl, String filePath) throws IOException {
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        HttpURLConnection connection = null;

        try {
            URL url = new URL(presignedUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("PUT");
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "text/plain");

            // 写入文件内容到连接输出流
            try (OutputStream os = connection.getOutputStream()) {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = fis.read(buffer)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
            }
            // 获取响应代码
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println("File uploaded successfully!");
            } else {
                System.out.println("Failed to upload file. HTTP Response Code: " + responseCode);
            }
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
}
